
#include <stdio.h>          // 标准输入输出
#include <stdint.h>         // 几种扩展的整数类型和宏
#include <stdlib.h>         // 标准函数库
#include <string.h>         // 字符串处理(操作字符数组)
#include "ohos_init.h"          // 用于初始化服务(services)和功能(features)
#include "cmsis_os2.h"          // CMSIS-RTOS API V2

#include "drv_adcMQ2.h"
#include "common.h"
#include "wifi_connecter.h"     // easy wifi (station模式)
#include "bmsinfo.h"
#include "adc.h"

uint32_t g_gas_data = 0;


void InitMQ2(void)
{
    uapi_adc_init(ADC_CLOCK_NONE);
}


void adc_readMQ2_Handle(void *arg)
{
    (void)arg;
    char strline[32] = {0};
    _SystemDataStru *bmsInf = getBmsInf();

    InitMQ2();

    while (1)
    {
        //读取MQ-2传感器数值（ADC4）
        adc_port_read(4, &g_gas_data);
        bmsInf->env_mq2 = g_gas_data;
        snprintf(strline, sizeof(strline), "MQ2: %04d", g_gas_data);
        OledShowString(2, 4, strline, 1);
        // DebugMsg("\r[long] MQ2 gas data = %d\n", g_gas_data);

        osDelay(100);
    }
}

void AdcMQ2Entry(void)
{
    // 定义线程属性
    osThreadAttr_t attr;
    attr.name = "adcMQ2Task";
    attr.stack_size = 2048;  
    attr.priority = osPriorityNormal;
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;

    // 创建线程
    if (osThreadNew(adc_readMQ2_Handle, NULL, &attr) == NULL)
    {
       printf("[AdcMQ2Entry] Falied to create Task ... \n");
    }
}

